﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class PrintCertByAdminEN : System.Web.UI.Page
{
    public Persons mobjUser
    {
        get
        {
            if (ViewState["mobjUser"] == null)
                ViewState["mobjUser"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["mobjUser"].ToString()] as Persons;
        }
        set
        {
            if (ViewState["mobjUser"] == null)
                ViewState["mobjUser"] = System.Guid.NewGuid().ToString();
            Session[ViewState["mobjUser"].ToString()] = value;
        }
    }



    public List<FinishedExams> lstGrid
    {
        get
        {
            if (ViewState["lstGrid"] == null)
                ViewState["lstGrid"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["lstGrid"].ToString()] as List<FinishedExams>;
        }
        set
        {
            if (ViewState["lstGrid"] == null)
                ViewState["lstGrid"] = System.Guid.NewGuid().ToString();
            Session[ViewState["lstGrid"].ToString()] = value;
            
        }
    }



    /// <summary>
    /// this function will check the security and customize the page
    /// </summary>
    private void checkAndCustomizeSecurity()
    {
        mobjUser = GlobalVars.getCurrentPerson();

        if (!mobjUser.lstAllRulesIDs.Contains((long)RulesEnum.Admin_page))
        {
            Response.Redirect("LoginEN.aspx");

        }

        



    }



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            checkAndCustomizeSecurity();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string badge = txtSearch.Text;

        PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);

        List<Persons> lstPersons = objPersonsDB.getPersonsByBadge(badge);

        if (lstPersons.Count == 0)
        {
            lstGrid = new List<FinishedExams>();

            grdCourses.DataSource = lstGrid;
            grdCourses.DataBind();
        }
        else
        {
            lstGrid = new List<FinishedExams>();
            
            foreach (Persons pn in lstPersons)
            {
                foreach (RegisteredCourses rc in pn.lstRegisteredCourses)
                {
                    List<FinishedExams> lstFinished = GlobalVars.getAllSucceedFinishedExam(rc.ID);
                    lstGrid.AddRange(lstFinished);
                }
            }

            grdCourses.DataSource = lstGrid;
            grdCourses.DataBind();
        }

    }
    protected void grdCourses_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCourses.DataSource = lstGrid;
        grdCourses.PageIndex = e.NewPageIndex;
        grdCourses.DataBind();
    }
}